package com.erp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.common.R;
import com.erp.dao.CashDao;
import com.erp.dao.GeneralLedgerDao;
import com.erp.entity.Cash;
import com.erp.service.CashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CashServiceImpl extends ServiceImpl<CashDao,Cash> implements CashService {

    @Autowired
    private CashDao cashDao;

    @Autowired
    private GeneralLedgerDao generalLedgerDao;

    @Override
    @Transactional
    public R<String> saveCashAndLedger(Cash cash) {

//        String type = cash.getType();
//
//        Specification<GeneralLedger> specification = (root, query, criteriaBuilder)
//                -> criteriaBuilder.equal(root.get("type").as(String.class), type);
//
//        if (!generalLedgerDao.findOne(specification).isPresent()){
//            GeneralLedger generalLedger = new GeneralLedger();
//            generalLedger.setType(type);
//            generalLedger.setMoney(cash.getMoney());
//            generalLedgerDao.save(generalLedger);
//        }else {
//            GeneralLedger generalLedger = generalLedgerDao.findOne(specification).get();
//            generalLedger.setMoney(generalLedger.getMoney()+cash.getMoney());
//            generalLedgerDao.save(generalLedger);
//        }
        this.save(cash);
        return R.success("现金流新增成功");
    }

    @Override
    @Transactional
    public R<String> deleteCashAndUpdateLedger(Cash cash) {
//        String type = cash.getType();
//
//        Specification<GeneralLedger> specification = (root, query, criteriaBuilder)
//                -> criteriaBuilder.equal(root.get("type").as(String.class), type);
//
//        if (generalLedgerDao.findOne(specification).isPresent()){
//            GeneralLedger generalLedger = generalLedgerDao.findOne(specification).get();
//            generalLedger.setMoney(generalLedger.getMoney()-cash.getMoney());
//            generalLedgerDao.save(generalLedger);
//        }
//        cashDao.deleteById(cash.getId());
        return R.success("删除成功");
    }
}
